package cn.itrip.auth.controller;
import cn.itrip.auth.service.RegisterService;
import cn.itrip.dto.Dto;
import cn.itrip.pojo.ItripUser;
import cn.itrip.utlis.DtoUtils;
import cn.itrip.utlis.ErrorCode;
import cn.itrip.utlis.MD5Utils;
import cn.itrip.vo.userinfo.ItripUserVO;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
/**
 * Created by LENOVO on 2018/8/17.
 */
@Controller
@RequestMapping("/api")
public class RegisterController {
     @Resource
     private RegisterService registerService;
    /**
     * 手机注册
     * @param itripUserVO
     * @return
     */
    @ApiOperation(value="手机注册功能API",httpMethod = "POST", protocols = "HTTP", produces = "application/json", response = Dto.class,notes="使用手机注册")
    @RequestMapping(value = "/registerbyphone",method = RequestMethod.POST,produces = "application/json")
    @ResponseBody
    public Dto doregisterByPhone(@ApiParam(name="itripUserVO",value="用户信息",required=true)@RequestBody ItripUserVO itripUserVO)
    {
        String phoneReg="^1[3578]{1}\\d{9}$";
        if(itripUserVO.getUserCode().matches(phoneReg))
        {
            try {
                if(!registerService.findUserByUserCode(itripUserVO.getUserCode()))
                 {
                    ItripUser itripUser=new ItripUser();
                    itripUser.setUsercode(itripUserVO.getUserCode());
                    itripUser.setUsername(itripUserVO.getUserName());
                    itripUser.setUserpassword(MD5Utils.getMD5(itripUserVO.getUserPassword(),32));
                    registerService.registerUserByPhone(itripUser);
                    return DtoUtils.returnSuccess("注册成功");
                 }
                else
                 {
                    return DtoUtils.returnFail("用户已存在", ErrorCode.AUTH_USER_ALREADY_EXISTS);
                 }
               }
              catch (Exception e) {
               e.printStackTrace();
                  return DtoUtils.returnFail("注册异常", ErrorCode.AUTH_UNKNOWN);
              }
        }
        else
        {
            return DtoUtils.returnFail("请输入正确手机号", ErrorCode.AUTH_ILLEGAL_USERCODE);
        }
    }

    /**
     * 手机短信验证
     * @param user
     * @param code
     * @return
     */
    @ApiOperation(value="手机短信验证API",httpMethod = "PUT", protocols = "HTTP", produces = "application/json", response = Dto.class,notes="使用手机短信验证")
    @RequestMapping(value = "/validate",method = RequestMethod.PUT,produces = "application/json")
    @ResponseBody
    public Dto dovalidateSms(@ApiParam(name="user",value="用户名",required=true)@RequestParam String user,@ApiParam(name="code",value="短信验证码",required=true)@RequestParam String code){
        try {
            if(registerService.validateSms(user,code))
            {
                return DtoUtils.returnSuccess("验证成功");
            }
            else
            {
                return DtoUtils.returnSuccess("验证失败");
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            return DtoUtils.returnFail("验证失败", ErrorCode.AUTH_UNKNOWN);
        }
    }

    /**
     * 邮箱注册
     * @param itripUserVO
     * @return
     * @throws Exception
     */
    @ApiOperation(value="邮箱注册API",httpMethod = "POST", protocols = "HTTP", produces = "application/json", response = Dto.class,notes="使用邮箱注册")
    @RequestMapping(value = "/doregister",method = RequestMethod.POST,produces = "application/json")
    @ResponseBody
    public Dto doregisterByMail(@ApiParam(name="itripUserVO",value="用户信息",required=true)@RequestBody ItripUserVO itripUserVO) throws Exception {

        String mailRege="^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$";
        if(itripUserVO.getUserCode().matches(mailRege))
        {
            try {
                if(!registerService.findUserByUserCode(itripUserVO.getUserCode()))
                {
                    ItripUser itripUser=new ItripUser();
                    itripUser.setUsercode(itripUserVO.getUserCode());
                    itripUser.setUsername(itripUserVO.getUserName());
                    itripUser.setUserpassword(MD5Utils.getMD5(itripUserVO.getUserPassword(),32));
                    registerService.registerUserByMail(itripUser);
                    return DtoUtils.returnSuccess("注册成功");
                }
                else
                {
                    return DtoUtils.returnFail("用户已存在", ErrorCode.AUTH_UNKNOWN);
                }
            }
            catch (Exception e) {
                e.printStackTrace();
                return DtoUtils.returnFail("注册异常", ErrorCode.AUTH_UNKNOWN);
            }

        }
        else
        {
            return DtoUtils.returnFail("请输入正确邮箱", ErrorCode.AUTH_ILLEGAL_USERCODE);
        }
    }

    /**
     * 邮箱激活码验证
     * @param user
     * @param code
     * @return
     */
    @ApiOperation(value="邮箱激活码验证API",httpMethod = "PUT", protocols = "HTTP", produces = "application/json", response = Dto.class,notes="使用邮箱激活码验证")
    @RequestMapping(value = "/activate",method = RequestMethod.PUT,produces = "application/json")
    @ResponseBody
    public Dto dovActivateMail(@ApiParam(name="user",value="用户名",required=true)@RequestParam String user,@ApiParam(name="code",value="邮箱激活码",required=true)@RequestParam String code){

        try {
            if(registerService.activateMail(user,code))
            {
                return DtoUtils.returnSuccess("激活成功");
            }
            else
            {
                return DtoUtils.returnSuccess("激活失败");
            }
         }
        catch (Exception e) {
            e.printStackTrace();
            return DtoUtils.returnFail("激活失败", ErrorCode.AUTH_UNKNOWN);
        }
    }

    /**
     * 用户名验证
     * @param name
     * @return
     */
    @ApiOperation(value="用户名验证API",httpMethod = "GET", protocols = "HTTP", produces = "application/json", response = Dto.class,notes="使用用户名验证")
    @RequestMapping(value = "/ckusr",method = RequestMethod.GET,produces = "application/json")
    @ResponseBody
    public Dto ckuser(@ApiParam(name="name",value="用户名",required=true)@RequestParam String name) {
        try {
            if (registerService.findUserByUserCode(name)) {
                return DtoUtils.returnFail("用户已存在", ErrorCode.AUTH_USER_ALREADY_EXISTS);
            } else {
                return DtoUtils.returnSuccess("用户名不存在,可以使用");
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            return DtoUtils.returnFail("检测失败", ErrorCode.AUTH_UNKNOWN);
        }
    }
}
